
package com.ray.tcc.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.ray.tcc.service.ApplicationService;
import com.ray.tcc.table.entity.Application;
import com.ray.tcc.table.entity.ApplicationExample;
import com.ray.tcc.table.entity.ApplicationExample.Criteria;
import com.ray.tcc.table.mapper.ApplicationMapper;
import com.zjhc.flyhong.common.datastructure.TreeNode;
import com.zjhc.flyhong.common.function.enums.DeleteEnum;
import com.zjhc.flyhong.common.function.exception.BussinessException;
import com.zjhc.flyhong.common.function.operator.LoginUser;
import com.zjhc.flyhong.common.function.operator.PageDto;
import com.zjhc.flyhong.common.util.UUIDUtil;
import com.zjhc.flyhong.common.web.ErrorMsg;
import com.zjhc.flyhong.common.web.Result;
import com.zjhc.flyhong.common.web.SuccessMsg;
import com.zjhc.flyhong.common.web.base.BaseService;

/**
 * <p>Description: .</p>
 *
 * @author ray
 * @version 1.0
 * @date 2017/10/20 16:39
 * @since JDK 1.8
 */
@Service
public class ApplicationServiceImpl extends BaseService implements ApplicationService {

	/**日志对象**/
	private static Logger logger = Logger.getLogger(ApplicationServiceImpl.class);
	
	@Autowired
	private ApplicationMapper applicationMapper;

	/**
	 * 新增
	 */
	@Transactional
	public Result<Object> addApplication(Application params, LoginUser user) {
		Result<Object> result=null;
		result=new Result<Object>(0,SuccessMsg.OP_SUCCESS);
		params.setAppId(UUIDUtil.getUUID());
		params.setIsDelete(DeleteEnum.USE.getValue());
		params.setTenantId(user.getTenantId());
		this.setUserForAdd(params, user);
		int count=applicationMapper.insertSelective(params);
		if(count!=1){
			logger.error("ApplicationServiceImpl=>addApplication is error");
			throw new BussinessException(-1, ErrorMsg.INSERT_ERROR);
		}
		return result;
	}

	@Transactional(propagation=Propagation.NOT_SUPPORTED)
	public Application queryApplication(String id, LoginUser user) {
		return applicationMapper.selectByPrimaryKey(id);
	}

	@Transactional
	public Result<Object> updateApplication(Application params, LoginUser user) {
		Result<Object> result=null;
		result=new Result<Object>(0,SuccessMsg.OP_SUCCESS);
		this.setUserForUpdate(params, user);
		int count=applicationMapper.updateByPrimaryKeySelective(params);
		if(count!=1){
			logger.error("ApplicationServiceImpl=>updateApplication is error");
			throw new BussinessException(-1, ErrorMsg.UPDATE_ERROR);
		}
		return result;
	}

	@Transactional
	public Result<Object> deleteApplication(String id, LoginUser user) {
		Result<Object> result=null;
		result=new Result<Object>(0,SuccessMsg.OP_SUCCESS);
		Application params=new Application();
		params.setAppId(id);
		params.setIsDelete(DeleteEnum.DELETE.getValue());
		this.setUserForUpdate(params, user);
		int count=applicationMapper.updateByPrimaryKeySelective(params);
		if(count!=1){
			logger.error("ApplicationServiceImpl=>deleteApplication is error");
			throw new BussinessException(-1, ErrorMsg.DELETE_ERROR);
		}
		return result;
	}

	@Transactional(propagation=Propagation.NOT_SUPPORTED)
	public PageDto<Application> queryApplication(Application params, PageDto<Application> page, LoginUser user) {
		ApplicationExample example=new ApplicationExample();
		Criteria criteria =  example.createCriteria();
		if(!user.isSuperManager()){
			criteria.andTenantIdEqualTo(user.getTenantId());
		}
		criteria.andIsDeleteEqualTo(DeleteEnum.USE.getValue());
		page.setTotal(applicationMapper.countByExample(example));
		page.setRecords(applicationMapper.selectByExample(example));
		return page;
	}
	
	@Transactional(propagation=Propagation.NOT_SUPPORTED)
	public List<TreeNode> queryApplicationList(LoginUser user) {
		List<TreeNode> list = new ArrayList<TreeNode>();
		Application params = new Application();
		if(!user.isSuperManager()){
			params.setTenantId(user.getTenantId());
		}
		params.setIsDelete(DeleteEnum.USE.getValue());
		List<Application> apps = applicationMapper.selectByRecord(params);
		TreeNode node = null;
		for(Application app : apps){
			node = new TreeNode(app.getAppId(),app.getAppName(),0, false);
			node.setValue(app);
			list.add(node);
		}
		return list;
	}

}
